﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace bttuan5_hdh_bai2
{
    public partial class Form1 : Form
    {
        DirectoryInfo curDir;
        public Form1()
        {
            InitializeComponent();

            // them cot vao listview
            ltv_thu_muc.Columns.Add("Name", 200, HorizontalAlignment.Left);
            ltv_thu_muc.Columns.Add("Size", 80, HorizontalAlignment.Right);
            ltv_thu_muc.Columns.Add("Type", 80, HorizontalAlignment.Left);
            ltv_thu_muc.Columns.Add("Date Modified", 160, HorizontalAlignment.Left);
            // hien thi theo dang chi tiet
            ltv_thu_muc.View = View.Details;
            // them danh sach hinh cho icon cua list view
            ltv_thu_muc.SmallImageList = new ImageList();
            ltv_thu_muc.SmallImageList.Images.Add(new Icon("icons/thu_muc.ico"));
            ltv_thu_muc.SmallImageList.Images.Add(new Icon("icons/tap_tin.ico"));

        }

        private void OpenDirectory()
        {
            tb_dia_chi.Text = curDir.FullName;
            ltv_thu_muc.Items.Clear();
            foreach (DirectoryInfo subDir in curDir.GetDirectories())
            {
                ListViewItem lvi = ltv_thu_muc.Items.Add(subDir.Name);
                lvi.Tag = subDir;
                lvi.ImageIndex = 0;
                lvi.SubItems.Add("");
                lvi.SubItems.Add("Folder");
                lvi.SubItems.Add(subDir.LastWriteTime.ToString());
            }
            foreach (FileInfo file in curDir.GetFiles())
            {
                ListViewItem lvi = ltv_thu_muc.Items.Add(file.Name);
                lvi.Tag = file;
                lvi.ImageIndex = 1;
                lvi.SubItems.Add(file.Length.ToString());
                lvi.SubItems.Add("File");
                lvi.SubItems.Add(file.LastWriteTime.ToString());
            }
        }


        private void btn_go_Click(object sender, EventArgs e)
        {
            curDir = new DirectoryInfo(tb_dia_chi.Text);
            OpenDirectory();
        }

        private void ltv_thu_muc_ItemActivate(object sender, EventArgs e)
        {
            if (ltv_thu_muc.SelectedItems[0].Tag.GetType() == typeof(DirectoryInfo))
            {
                curDir = (DirectoryInfo)ltv_thu_muc.SelectedItems[0].Tag;
                OpenDirectory();
            }
            else
            {
                FileInfo file = (FileInfo)ltv_thu_muc.SelectedItems[0].Tag;
                System.Diagnostics.Process.Start(file.FullName);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int i = 0;
            foreach (DriveInfo d in DriveInfo.GetDrives())
            {
                Button btnDrive = new Button();
                btnDrive.Text = d.Name;
                btnDrive.Tag = d;
                btnDrive.Size = new Size(50, 25);
                btnDrive.Location = new Point(55 * i, 10);
                btnDrive.Click += new EventHandler(btnDrive_Click);

                pnl.Controls.Add(btnDrive);
                i++;
            }
        }

        void btnDrive_Click(object sender, EventArgs e)
        {
            Button btnDrive = (Button)sender;
            tb_dia_chi.Text = btnDrive.Text;
            curDir = new DirectoryInfo(tb_dia_chi.Text);
            OpenDirectory();
        }

        private void view_Click(object sender, EventArgs e)
        {
            ltv_thu_muc_ItemActivate(sender, e);
        }

        private void edit_Click(object sender, EventArgs e)
        {
            ltv_thu_muc_ItemActivate(sender, e);
        }

        private void newfol_Click(object sender, EventArgs e)
        {
            newfolder_form nfd_form = new newfolder_form();
            var result = nfd_form.ShowDialog();
            if (result == DialogResult.OK)
            {
                if (tb_dia_chi.Text.EndsWith("\\"))
                {
                    Directory.CreateDirectory(tb_dia_chi.Text + nfd_form.folder_name);
                }
                else
                    Directory.CreateDirectory(tb_dia_chi.Text + "\\" + nfd_form.folder_name);
            }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            if (ltv_thu_muc.SelectedItems[0].Tag.GetType() == typeof(DirectoryInfo))
            {
                curDir = (DirectoryInfo)ltv_thu_muc.SelectedItems[0].Tag;
                RecursiveDelete(tb_dia_chi.Text, curDir.Name);
                
            }
            else
            {
                FileInfo file = (FileInfo)ltv_thu_muc.SelectedItems[0].Tag;
                File.Delete(file.FullName);
            }
            btn_go_Click(sender, e);
        }

        private void RecursiveDelete(string path, string name)
        {
            foreach (string directory in Directory.GetDirectories(path))
            {
                if (directory.EndsWith("\\" + name))
                {
                    Directory.Delete(directory, true);
                }
                else
                {
                    RecursiveDelete(directory, name);
                }
            }
        }
    }
}
